package com.six.epidemic.report.service.impl

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.six.epidemic.report.pojo.EpidemicReport
import com.six.epidemic.report.pojo.vo.SearchEpidemicReport
import com.six.epidemic.report.mapper.EpidemicReportMapper
import com.six.epidemic.report.service.IEpidemicReportService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
import java.util.function.Consumer

@Service
class EpidemicReportService : IEpidemicReportService {
    @Autowired
    private lateinit var mapper: EpidemicReportMapper


    override fun add(model: EpidemicReport): Int {
        model.reportTime = Date()
        // 同一个人，在10分钟内不能重复提交
        val queryWrapper: QueryWrapper<EpidemicReport> = QueryWrapper<EpidemicReport>().apply {

            this.eq("identityCard", model.identityCard)
            this.orderByDesc("reporttime")
        }

        val oldlist = mapper.selectList(queryWrapper)

        if (oldlist != null && oldlist.size > 0) {
            if (model.reportTime.time < oldlist[0].reportTime.time + 10 * 60 * 1000) return -10
        }
        return mapper.insert(model)
    }

    override fun getPageListByQuery(model: SearchEpidemicReport): List<EpidemicReport> {
        var model = model
        if (model != null) {
            //数据处理
            if ("" == model.province || "null" == model.province || "请选择" == model.province) model.province = ""

            if ("" == model.city || "null" == model.city || "请选择" == model.city) model.city = ""
            if ("" == model.district || "null" == model.district || "请选择" == model.district) model.district = ""
            if ("" == model.startTime || "null" == model.startTime || "请选择" == model.startTime) model.startTime = ""
            if ("" == model.endTime || "null" == model.endTime || "请选择" == model.endTime) model.endTime = ""
        } else {
            model = SearchEpidemicReport()
        }
        val list = mapper.getPageListByQuery(model)
        list.forEach(Consumer { item: EpidemicReport -> item.address = item.provinces.replace("|", "") + item.address })
        return list
    }

    override fun findAll(): List<EpidemicReport> {
        return mapper.findAll()
    }

}